package com.example.hzz.simpleplayer;

import android.app.Fragment;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by huangzezhan on 2018/9/11.
 */

public class PlayerFragment extends Fragment {
    private static final String TAG = "PlayerFragment";
    private SurfaceView sf;
    private MediaPlayer mp;
    private Button bPlay, bStop, bPause;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
    }

    public static PlayerFragment newInstance() {
        PlayerFragment pf = new PlayerFragment();
        return pf;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_player_layout, container, false);
        bPlay = v.findViewById(R.id.play_id);
        bStop = v.findViewById(R.id.stop_id);
        bPause = v.findViewById(R.id.pause_id);
        bPlay.setOnClickListener(listener);
        bStop.setOnClickListener(listener);
        bPause.setOnClickListener(listener);
        mp = new MediaPlayer();
        sf = v.findViewById(R.id.playerSuface);
        sf.getHolder().addCallback(callback);
        sf.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        return v;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.play_id:
                    videoPlay();
                    break;
                case R.id.stop_id:
                    videoStop();
                    break;
                case R.id.pause_id:
                    videoPause();
                    break;
            }
        }
    };


    SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            Log.i(TAG, "surfaceCreated");
            setSource();

        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            Log.i(TAG, "surfaceChanged");
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            Log.i(TAG, "surfaceDestroyed");
            videoStop();
        }
    };

    private void setSource() {
        Log.i(TAG, "videoPlay");
        try {
            mp.reset();
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setDataSource("/sdcard/airpods.mp4");
            mp.setDisplay(sf.getHolder());
        } catch (Exception e) {

        }
    }

    private void videoPlay() {
        Log.i(TAG, "videoPlay");
        try {
            mp.prepare();
            mp.start();
        } catch (Exception e) {

        }
    }

    private void videoStop() {
        Log.i(TAG, "videoStop");
        if (mp != null) {
            mp.stop();
            mp.release();
            mp = null;
            this.getActivity().finish();
        }
    }

    private void videoPause() {
        Log.i(TAG, "videoPause");
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.pause();
            } else {
                mp.start();
            }
        }
    }
}
